package com.beauty.dressing.module.main;

import android.content.res.Configuration;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;

import com.beauty.dressing.R;
import com.beauty.dressing.common.base.BaseFragment;
import com.beauty.dressing.common.view.NoScrollViewPager;
import com.beauty.dressing.module.main.fragment.CourseFragment;
import com.beauty.dressing.module.main.fragment.MyCourseFragment;
import com.framework.common.base.IFragmentPagerAdapter;
import com.framework.common.utils.ILog;
import com.framework.common.view.tablayout.CommonTabLayout;
import com.framework.common.view.tablayout.bean.BaseTabEntity;
import com.framework.common.view.tablayout.listener.CustomTabEntity;
import com.framework.common.view.tablayout.listener.OnTabSelectListener;

import java.util.ArrayList;
import java.util.List;

/**
 * 首页
 * @author xutingz
 * @E-mail xutz@xianlanba.com
 * @date 2018/05/02
 */

public class MainFragment extends BaseFragment implements OnTabSelectListener {
    public static final int TAG_RANK = 0;
    public static final int TAG_MINE = 1;

    private CommonTabLayout mTabLayout;
    public NoScrollViewPager mViewPager;
    private BaseFragment[] fragments;
    private boolean isShowDot = false;
    private int lastSelection = 0;
    private ViewGroup mTopTabLayout;
    @Override
    public void setContentView() {
        setContentView(R.layout.main_fragment);
    }

    @Override
    public void findView() {
        mTopTabLayout = (ViewGroup) findViewById(R.id.top_tab_layout);
        mTabLayout = (CommonTabLayout) findViewById(R.id.tab_layout);
        mViewPager = (NoScrollViewPager) findViewById(R.id.viewpager);
        mTabLayout.setOnTabSelectListener(this);
        ArrayList<CustomTabEntity> list = new ArrayList<>();
        list.add(new BaseTabEntity("课程排行"));
        list.add(new BaseTabEntity("我的课程"));
        mTabLayout.setTabData(list);
        findViewById(R.id.first_search).setOnClickListener(this);
        findViewById(R.id.left_btn).setOnClickListener(this);
    }

    @Override
    public void initData() {
        fragments = new BaseFragment[3];
        fragments[TAG_RANK] = new CourseFragment();
        fragments[TAG_MINE] = new MyCourseFragment();
        List<BaseFragment> fragmentList = new ArrayList<>();
        fragmentList.add(fragments[TAG_RANK]);
        fragmentList.add(fragments[TAG_MINE]);
        IFragmentPagerAdapter mPagerAdapter = new IFragmentPagerAdapter(getChildFragmentManager());
        mPagerAdapter.setList(fragmentList);
        mViewPager.setAdapter(mPagerAdapter);
        mViewPager.setOffscreenPageLimit(fragmentList.size());
        mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int i, float v, int i1) {

            }

            @Override
            public void onPageSelected(int position) {
                if (position != mTabLayout.getCurrentTab()) {
                    mTabLayout.setCurrentTab(position);
                    onTabFocusChange(position);
                }
            }

            @Override
            public void onPageScrollStateChanged(int i) {

            }
        });
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.first_search:
                //TODO 跳转到搜索
                break;
            case R.id.left_btn:
                context.finish();
                break;
        }
    }

    @Override
    public void onTabSelect(int position) {
        if (position != mViewPager.getCurrentItem()) {
            mViewPager.setCurrentItem(position, true);
        }
        onTabFocusChange(position);
    }

    public boolean isFirstPageSelect(){
        return mViewPager!=null && mViewPager.getCurrentItem()== TAG_RANK;
    }

    private void onTabFocusChange(int position){
        lastSelection = position;
    }

    @Override
    public void onTabReselect(int position) {

    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        fragments[TAG_RANK].onDestroyView();
        fragments[TAG_MINE].onDestroyView();
    }


    /**
     * 在首页，点击首页图片刷新
     */
    public void homeTabRefresh(){
        if (lastSelection==0){
            ((CourseFragment)fragments[lastSelection]).homeTabRefresh();
        }else if (lastSelection==1){
            ((MyCourseFragment)fragments[lastSelection]).homeTabRefresh();
        }
    }


    @Override
    public void hide() {
        super.hide();
        fragments[TAG_RANK].hide();
        fragments[TAG_MINE].hide();
    }

    @Override
    public void show() {
        super.show();
        fragments[lastSelection].show();
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) { // 横竖屏切换
        super.onConfigurationChanged(newConfig);
        boolean isLandscape = Configuration.ORIENTATION_LANDSCAPE == newConfig.orientation;
        if (mViewPager!=null && mTopTabLayout!=null) {
            mViewPager.setNoScroll(isLandscape);
            mTopTabLayout.setVisibility(isLandscape ? View.GONE : View.VISIBLE);
        }
    }
}
